package com.lefeng.cockpit.datax.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lefeng.cockpit.common.bean.po.job.JobInfoPO;
import com.lefeng.cockpit.datax.dao.JobInfoDao;
import com.lefeng.cockpit.datax.dao.mapper.JobInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class JobInfoDaoImpl implements JobInfoDao {

    @Resource
    private JobInfoMapper jobInfoMapper;

    @Override
    public JobInfoPO loadById(int jobId) {
        return jobInfoMapper.selectById(jobId);
    }

    @Override
    public void updateLastHandleCode(int jobId, int lastHandleCode) {
        JobInfoPO jobInfoPO = new JobInfoPO();
        jobInfoPO.setId(jobId);
        jobInfoPO.setLastHandleCode(lastHandleCode);
        LambdaUpdateWrapper<JobInfoPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JobInfoPO::getId, jobInfoPO.getId()); // WHERE 条件
        updateWrapper.set(JobInfoPO::getLastHandleCode, jobInfoPO.getLastHandleCode()); // 更新值
        jobInfoMapper.update(jobInfoPO, updateWrapper);
    }

    @Override
    public void incrementIdUpdate(int jobId, Long incStartId) {
        JobInfoPO jobInfoPO = new JobInfoPO();
        jobInfoPO.setId(jobId);
        jobInfoPO.setIncStartId(incStartId);
        LambdaUpdateWrapper<JobInfoPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JobInfoPO::getId, jobInfoPO.getId()); // WHERE 条件
        updateWrapper.set(JobInfoPO::getIncStartId, jobInfoPO.getIncStartId()); // 更新值
        jobInfoMapper.update(jobInfoPO, updateWrapper);
    }

    @Override
    public void incrementTimeUpdate(int jobId, Date incStartTime) {
        JobInfoPO jobInfoPO = new JobInfoPO();
        jobInfoPO.setId(jobId);
        jobInfoPO.setIncStartTime(incStartTime);
        LambdaUpdateWrapper<JobInfoPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JobInfoPO::getId, jobInfoPO.getId()); // WHERE 条件
        updateWrapper.set(JobInfoPO::getIncStartTime, jobInfoPO.getIncStartTime()); // 更新值
        jobInfoMapper.update(jobInfoPO, updateWrapper);
    }

    /**
     * 原符号       <       <=      >       >=      <>
     * 对应函数    lt()     le()    gt()    ge()    ne()
     */
    @Override
    public List<JobInfoPO> scheduleJobQuery(long maxNextTime, int pageSize) {
        LambdaQueryWrapper<JobInfoPO> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(JobInfoPO::getTriggerStatus, 1)
                .le(JobInfoPO::getTriggerNextTime, maxNextTime)//小于等于
                .orderByAsc(JobInfoPO::getId).last("limit " + pageSize);
        return jobInfoMapper.selectList(wrapper);
    }

    @Override
    public void scheduleUpdate(JobInfoPO jobInfo) {
        LambdaUpdateWrapper<JobInfoPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JobInfoPO::getId, jobInfo.getId()); // WHERE 条件
        updateWrapper.set(JobInfoPO::getTriggerStatus, jobInfo.getTriggerStatus()); // WHERE 条件
        updateWrapper.set(JobInfoPO::getTriggerLastTime, jobInfo.getTriggerLastTime()); // 更新值
        updateWrapper.set(JobInfoPO::getTriggerNextTime, jobInfo.getTriggerNextTime()); // 更新值
        jobInfoMapper.update(jobInfo, updateWrapper);
    }
}
